<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class dosenajar extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_dosen');
        $this->load->model('m_tahunajar');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/dosenajar/index.html");
        // tahun ajar
        $rs_tahunajar = $this->m_tahunajar->get_detail_data_tahunajar();
        $this->smarty->assign('rs_tahunajar', $rs_tahunajar);
        // get data
        $rs_id = $this->m_dosen->get_data_group_dosenajar();
        $this->smarty->assign('rs_id', $rs_id);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses penambahan grup
    public function add_group_process() {
        // set page rules
        $this->_set_page_rule("C");
        // tahun ajaran aktif
        $rs_tahunajar = $this->m_tahunajar->get_detail_data_tahunajar();
        if (empty($rs_tahunajar)) {
            // default error
            $this->tnotification->sent_notification("error", "Tahun Ajaran belum di setting !");
            // default redirect
            redirect('pengaturan/dosenajar');
        }
        // cek 
        $cek_group = $this->m_dosen->is_setting_dosenajar_exist(array($rs_tahunajar['tahun'], $rs_tahunajar['semester']));
        //--
        if ($cek_group == 0) {
            //params
            $tahun = $rs_tahunajar['tahun'];
            $semester = $rs_tahunajar['semester'];
            //--
            $params = array($semester, $tahun, $this->com_user['user_id']);
            // insert
            if ($this->m_dosen->insert_setting_dosenajar($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Pengaturan baru telah dibuat!");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan !");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Pengaturan untuk semester ini sudah ada !");
        }
        // default redirect
        redirect('pengaturan/dosenajar');
    }

    // halaman edit group
    public function edit_group($set_dosenajar_kd = "") {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/dosenajar/detail.html");
        // detail grup
        $rs_group = $this->m_dosen->get_detail_setting_dosenajar($set_dosenajar_kd);
        $this->smarty->assign('rs_group', $rs_group);
        // data session
        $search = $this->tsession->userdata('detail_set_dosenajar');
        $this->smarty->assign('search', $search);
        // params
        $nama_lengkap = !empty($search['nama_lengkap']) ? "%" . $search['nama_lengkap'] . "%" : "%";
        //--
        $params = array($set_dosenajar_kd, $nama_lengkap);
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("pengaturan/dosenajar/edit_group/" . $set_dosenajar_kd . "/index/");
        $config['total_rows'] = $this->m_dosen->get_total_data_set_dosenajar_detail($params);
        $config['uri_segment'] = 6;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(6, 0) + 1;
        $end = $this->uri->segment(6, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($set_dosenajar_kd, $nama_lengkap, ($start - 1), $config['per_page']);
        $result = $this->m_dosen->get_data_set_dosenajar_detail($params);
        $this->smarty->assign('rs_id', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses search detail setting dosenajar 
    public function search_process() {
        // set page rules
        $this->_set_page_rule("R");
        //--
        if ($this->input->post('save') == 'Cari') {
            $params = array(
                "nama_lengkap" => $this->input->post('nama_lengkap')
            );
            // set session
            $this->tsession->set_userdata('detail_set_dosenajar', $params);
        } else {
            // set session
            $this->tsession->set_userdata('detail_set_dosenajar');
        }
        // rediredt
        redirect('pengaturan/dosenajar/edit_group/' . $this->input->post('set_dosenajar_kd'));
    }

    // halaman tambah dosen ajar
    public function add_dosenajar($set_dosenajar_kd = "") {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/dosenajar/add_dosenajar.html");
        // load javascript
        $this->smarty->load_javascript("resource/js/jquery/select2-3.4.5/select2.min.js");
        // load css 
        $this->smarty->load_style("select2/select2.css");
        // kode set
        $this->smarty->assign('set_dosenajar_kd', $set_dosenajar_kd);
        // tahun ajar aktif
        $rs_tahunajar = $this->m_tahunajar->get_detail_data_tahunajar();
        // get dosen aktif
        $this->smarty->assign('rs_dosen', $this->m_dosen->get_data_dosen_aktif());
        // get makul aktif
        $this->smarty->assign('rs_makul', $this->m_dosen->get_data_makul_aktif(array($rs_tahunajar['tahun'], $rs_tahunajar['semester'])));
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah dosen ajar
    public function add_dosenajar_process() {
        // set page rules
        $this->_set_page_rule("C");
        // cek input
        $this->tnotification->set_rules('set_dosenajar_kd', 'Kode Grup', 'required');
        $this->tnotification->set_rules('dosen_kd', 'Dosen', 'required');
        $this->tnotification->set_rules('makul_kd', 'Mata Kuliah', 'required');
        //--
        $set_dosenajar_kd = $this->input->post('set_dosenajar_kd');
        $dosen_kd = $this->input->post('dosen_kd');
        $makul_kd = $this->input->post('makul_kd');
        // cek dosen ajar
        if ($this->m_dosen->is_data_dosenajar_exist(array($set_dosenajar_kd, $dosen_kd, $makul_kd))) {
            // set error
            $this->tnotification->set_error_message("Data dosen ajar sudah ada !");
        }
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $params = array($set_dosenajar_kd, $dosen_kd, $makul_kd, $this->com_user['user_id']);
            // insert
            if ($this->m_dosen->insert_data_dosenajar($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        //redirect
        redirect('pengaturan/dosenajar/add_dosenajar/' . $this->input->post('set_dosenajar_kd'));
    }

    // halaman hapus dosen ajar
    public function delete_dosenajar($set_dosenajar_kd = "", $dosen_kd = "", $makul_kd = "") {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "pengaturan/dosenajar/delete_dosenajar.html");
        // load javascript
        $this->smarty->load_javascript("resource/js/jquery/select2-3.4.5/select2.min.js");
        // load css 
        $this->smarty->load_style("select2/select2.css");
        // tahun ajar aktif
        $rs_tahunajar = $this->m_tahunajar->get_detail_data_tahunajar();
        // get dosen aktif
        $this->smarty->assign('rs_dosen', $this->m_dosen->get_data_dosen_aktif());
        // get makul aktif
        $this->smarty->assign('rs_makul', $this->m_dosen->get_data_makul_aktif(array($rs_tahunajar['tahun'], $rs_tahunajar['semester'])));
        // data
        $this->smarty->assign('set_dosenajar_kd', $set_dosenajar_kd);
        $this->smarty->assign('dosen_kd', $dosen_kd);
        $this->smarty->assign('makul_kd', $makul_kd);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses tambah dosen ajar
    public function delete_dosenajar_process() {
        // set page rules
        $this->_set_page_rule("C");
        // cek input
        $this->tnotification->set_rules('set_dosenajar_kd', 'Kode Grup', 'required');
        $this->tnotification->set_rules('dosen_kd', 'Dosen', 'required');
        $this->tnotification->set_rules('makul_kd', 'Mata Kuliah', 'required');
        //--
        $set_dosenajar_kd = $this->input->post('set_dosenajar_kd');
        $dosen_kd = $this->input->post('dosen_kd');
        $makul_kd = $this->input->post('makul_kd');
        // proses
        if ($this->tnotification->run() !== FALSE) {
            // params
            $params = array($set_dosenajar_kd, $dosen_kd, $makul_kd);
            // delete
            if ($this->m_dosen->delete_data_dosenajar($params)) {
                // sukses
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil dihapus");
                // redirect
                redirect('pengaturan/dosenajar/edit_group/' . $set_dosenajar_kd);
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal dihapus");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal dihapus");
        }
        //redirect
        redirect('pengaturan/dosenajar/add_dosenajar/' . $set_dosenajar_kd . '/' . $dosen_kd . '/' . $makul_kd);
    }

}